package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.yangzihui.*;
import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.service.AdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author yangzihui
 * @Description
 * @Create 2022-06-26 14:28
 * @Version
 */
@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;

    @Autowired
    CouponMapper couponMapper;

    private String generateShortUuid() {
        String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
                "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z"};
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    @Override
    public CommonData query(BaseParam baseParam, String name, String content) {
        name = StringUtils.isBlank(name) ? null : ("%" + name + "%");
        content = StringUtils.isBlank(content) ? null : ("%" + content + "%");

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketAd> marketAds = adMapper.select(baseParam.getSort(), baseParam.getOrder(), name, content);
        PageInfo pageInfo = new PageInfo(marketAds);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAd update(MarketAd marketAd) {
        Date date = new Date();
        adMapper.update(marketAd, date);
        marketAd.setUpdateTime(date);
        return marketAd;
    }

    @Override
    public void delete(MarketAd marketAd) {
        adMapper.delete(marketAd);
    }

    @Override
    public Integer selectGoodsId(NewMarketAd newMarketAd) {
        MarketAd ad = new MarketAd();
        ad.setId(Integer.parseInt(newMarketAd.getLink()));
        Integer goodsId = ad.getId();
        Integer num = couponMapper.selectGoodsId(goodsId);
        return num;
    }

    @Override
    public ResponseOfNewMarketAd create(NewMarketAd newMarketAd) {
        Date date = new Date();
        adMapper.create(newMarketAd, date);
        ResponseOfNewMarketAd resp = new ResponseOfNewMarketAd();
        resp.setId(newMarketAd.getId());
        resp.setName(newMarketAd.getName());
        resp.setContent(newMarketAd.getContent());
        resp.setUrl(newMarketAd.getUrl());
        resp.setLink(newMarketAd.getLink());
        resp.setPosition(newMarketAd.getPosition());
        resp.setEnabled(newMarketAd.getEnabled());
        resp.setUpdateTime(date);
        resp.setAddTime(date);
        return resp;
    }

    @Override
    public CouponData list(BaseParam baseParam, String name, Integer type, Integer status) {
        name = StringUtils.isBlank(name) ? null : ("%" + name + "%");
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<Coupon> coupons = couponMapper.select(baseParam.getSort(), baseParam.getOrder(), name, type, status);
        PageInfo pageInfo = new PageInfo(coupons);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponData.data(total, pages, baseParam.getLimit(), baseParam.getPage(), coupons);
    }

    @Override
    public Coupon add(NewCoupon newCoupon) {
        Date date = new Date();
        String str = generateShortUuid();
        BONewCoupon boNewCoupon = new BONewCoupon();
        boNewCoupon.setName(newCoupon.getName());
        boNewCoupon.setDesc(newCoupon.getDesc());
        boNewCoupon.setTag(newCoupon.getTag());
        boNewCoupon.setTotal(newCoupon.getTotal());
        boNewCoupon.setDiscount(newCoupon.getDiscount());
        boNewCoupon.setMin(newCoupon.getMin());
        boNewCoupon.setLimit(newCoupon.getLimit());
        boNewCoupon.setType(newCoupon.getType());
        boNewCoupon.setStatus(newCoupon.getStatus());
        boNewCoupon.setGoodsType(newCoupon.getGoodsType());
        boNewCoupon.setGoodsValue(Arrays.toString(newCoupon.getGoodsValue()));
        boNewCoupon.setCode(newCoupon.getCode());
        boNewCoupon.setTimeType(newCoupon.getTimeType());
        boNewCoupon.setDays(newCoupon.getDays());
        boNewCoupon.setStartTime(newCoupon.getStartTime());
        boNewCoupon.setEndTime(newCoupon.getEndTime());
        couponMapper.addBONewCoupon(boNewCoupon,date,str);
        Coupon coupon = new Coupon();
        coupon.setId(boNewCoupon.getId());
        coupon.setName(newCoupon.getName());
        coupon.setDesc(newCoupon.getDesc());
        coupon.setTag(newCoupon.getTag());
        coupon.setTotal(Integer.parseInt(newCoupon.getTotal()));
        coupon.setDiscount(Integer.parseInt(newCoupon.getDiscount()));
        coupon.setMin(Integer.parseInt(newCoupon.getMin()));
        coupon.setLimit(Integer.parseInt(newCoupon.getLimit()));
        coupon.setType(newCoupon.getType());
        coupon.setStatus(newCoupon.getStatus());
        coupon.setGoodsType(newCoupon.getGoodsType());
        coupon.setGoodsValue(newCoupon.getGoodsValue());
        coupon.setCode(str);
        coupon.setTimeType(newCoupon.getTimeType());
        coupon.setDays(newCoupon.getDays());
        coupon.setStartTime(newCoupon.getStartTime());
        coupon.setEndTime(newCoupon.getEndTime());
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);
        return coupon;
    }

    @Override
    public void sub(Coupon coupon) {
        couponMapper.delete(coupon);
        couponMapper.updateCouponUser(coupon);
    }

    @Override
    public CouponUserData listUser(BaseParam baseParam, Integer couponId, Integer status, Integer userId) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<CouponUser> couponUsers = couponMapper.query(baseParam.getSort(), baseParam.getOrder(), couponId,status,userId);
        PageInfo pageInfo = new PageInfo(couponUsers);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponUserData.data(total, pages, baseParam.getLimit(), baseParam.getPage(), couponUsers);
    }

    @Override
    public FinalFinalCoupon modify(FinalCoupon finalCoupon) {
        Date date = new Date();
        Integer type = couponMapper.selectType(finalCoupon);
        Integer timeType = couponMapper.selectTimeType(finalCoupon);
        if (type == 2) {
            couponMapper.modify1(finalCoupon, date);
        }
        if (type != 2){
            String str = generateShortUuid();
            couponMapper.modify2(finalCoupon,date,str);
        }
        if (timeType == 0){
            couponMapper.modify3(finalCoupon);
        }
        if (timeType == 1){
            couponMapper.modify4(finalCoupon);
        }
        FinalFinalCoupon finalCoupon1 = new FinalFinalCoupon();
        finalCoupon1.setId(finalCoupon.getId());
        finalCoupon1.setName(finalCoupon.getName());
        finalCoupon1.setDesc(finalCoupon.getDesc());
        finalCoupon1.setTag(finalCoupon.getTag());
        finalCoupon1.setTotal(Integer.parseInt(finalCoupon.getTotal()));
        finalCoupon1.setDiscount(Integer.parseInt(finalCoupon.getDiscount()));
        finalCoupon1.setMin(Integer.parseInt(finalCoupon.getMin()));
        finalCoupon1.setLimit(Integer.parseInt(finalCoupon.getLimit()));
        finalCoupon1.setType(finalCoupon.getType());
        finalCoupon1.setStatus(finalCoupon.getStatus());
        finalCoupon1.setGoodsType(finalCoupon.getGoodsType());
        finalCoupon1.setGoodsValue(finalCoupon.getGoodsValue());
        finalCoupon1.setTimeType(finalCoupon.getTimeType());
        finalCoupon1.setDays(finalCoupon.getDays());
        finalCoupon1.setStartTime(finalCoupon.getStartTime());
        finalCoupon1.setEndTime(finalCoupon.getEndTime());
        finalCoupon1.setAddTime(finalCoupon.getAddTime());
        finalCoupon1.setUpdateTime(date);
        finalCoupon1.setDeleted(finalCoupon.getDeleted());
        return finalCoupon1;
    }

    @Override
    public Integer selectTimeType(Integer id) {
        Integer timeType = couponMapper.selectTimeType1(id);
        return timeType;
    }

    @Override
    public Coupon read(Integer id) {
        Coupon coupon = couponMapper.read(id);
        return coupon;
    }

    @Override
    public FinalCoupon newRead(Integer id) {
        FinalCoupon finalCoupon = couponMapper.newRead(id);
        return finalCoupon;
    }
}
